package com.z6school.manage.service.impl;



import com.z6school.manage.common.annotation.RequiredLog;
import com.z6school.manage.common.exception.ServiceException;

import com.z6school.manage.common.pojo.Nodes;
import com.z6school.manage.mapper.DeptMapper;

import com.z6school.manage.po.School;
import com.z6school.manage.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 查询 所有部门信息 封装到map 中 以集合形式带到前端
     * @return
     */
    @Override
    @RequiredLog(operation = "查询部门信息")
    public List<Map<String, Object>> findObjects() {
        List<Map<String, Object>> objects = deptMapper.findObjects();
        if(objects==null||objects.size()==0)
            throw new ServiceException("没有部门信息");
        return objects;
    }

    /**
     * 删除院校信息
     * @param id
     * @return
     */
    @Override
    @RequiredLog(operation = "删除部门信息")
    public int deleteObject(Integer id) {
        //1.校验参数
        if (id==null||id<1) throw  new IllegalArgumentException("当前id有误");
        //2。判定当前选中 是否有子菜单
        int childCount = deptMapper.findChildCount(id);
        if (childCount<1) throw new ServiceException("请先删除 系/班级");
        //3.删除学校信息
        int i = deptMapper.deleteObject(id);
        //4.校验
        if (i<1) throw new ServiceException("当前项可能已不存在");
        return i;
    }

    @Override
    @RequiredLog(operation = "新增部门信息")
    public int saveObject(School school) {
        //1.校验参数
        if (school==null) throw  new  IllegalArgumentException("请填写信息");
        int i = deptMapper.insertObject(school);
        return i;
    }

    @Override
    @RequiredLog(operation = "更新部门信息")
    public int updateObject(School school) {
        if (school==null) throw  new  IllegalArgumentException("请填写信息");
        int i = deptMapper.updateObject(school);
        return i;
    }

    @Override
    public School findById(Integer id) {
        School school = deptMapper.findById(id);
        return school;
    }
    @Override
    public List<Nodes> findZTreeNodes() {
        List<Nodes> list=
                deptMapper.findZTreeNodes();
        if(list==null||list.size()==0)
            throw new ServiceException("没有部门信息");
        return list;
    }

}
